using System;
using System.Collections.Generic;
using System.Text;
using MCMLXVII.BackEnd.Data.SystemDatabase;
using MCMLXVII.Core.Common.BackEnd;
using MCMLXVII.BackEnd.Pipeline;
using MCMLXVII.Core.Common;
using MCMLXVII.SubSystems.Security.DirectoryServices.QflowWS;
using System.Xml;

namespace MCMLXVII.SubSystems.Security.DirectoryServices
{
    public class Qflow
    {
        private const string PNQflowWSUrl = "QflowWebServicesLocation";
        private const string PNQflowWSUseIS = "QflowWebServicesIS";
        private const string PNQflowWSUserName = "QflowWebServicesUserName";
        private const string PNQflowWSPassword = "QflowWebServicesPassword";

        private static string _URL = "";
        private static bool _IntegratedSecurityMode = false;
        private static string _UserName = "";
        private static string _Password = "";

        public Qflow()
        {

        }


        private static void InitConfig()
        {

            _URL = Configuration.Instance.GetSystemParameter(PNQflowWSUrl);
            _IntegratedSecurityMode = bool.Parse(Configuration.Instance.GetSystemParameter(PNQflowWSUseIS));
            if (!_IntegratedSecurityMode)
            {
                _UserName = Configuration.Instance.GetSystemParameter(PNQflowWSUserName);
                _Password = Configuration.Instance.GetSystemParameter(PNQflowWSPassword);
            }
        }

        public static XmlDocument GetUsersList()
        {
            if (_URL == "")
                InitConfig();

            WebOrganization WS = new WebOrganization();
            WS.Url = _URL;
            if (_IntegratedSecurityMode)
            {
                WS.UseDefaultCredentials = true;
                WS.Credentials = System.Net.CredentialCache.DefaultCredentials;
            }
            else
            {
                WS.Credentials = new System.Net.NetworkCredential(_UserName, _Password);
            }

            StringBuilder sb = new StringBuilder();
            sb.Append("<Root Path=\"Qflow\">");

            User[] users = WS.GetUsers();
            foreach(User u in users)
            {
                sb.Append("<Node Name=\"" + u.Name + "\" ID=\"" + u.ID  + "\" LogonName=\"" + u.LogonUserName + "\" SecProvider=\"" + u.LogonProvider + "\" SecProviderAlt=\"" + u.LogonProviderAlt + "\" />");
            }

            sb.Append("</Root>");
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(sb.ToString());
            return xDoc;
        }

        public static XmlDocument GetUser(int UserID)
        {
            if (_URL == "")
                InitConfig();

            WebOrganization WS = new WebOrganization();
            WS.Url = _URL;
            if (_IntegratedSecurityMode)
            {
                WS.UseDefaultCredentials = true;
                WS.Credentials = System.Net.CredentialCache.DefaultCredentials;
            }
            else
            {
                WS.Credentials = new System.Net.NetworkCredential(_UserName, _Password);
            }

            StringBuilder sb = new StringBuilder();
            sb.Append("<Root Path=\"Qflow\">");

            User u = WS.GetUser(UserID);
            sb.Append("<Node Name=\"" + u.Name + "\" ID=\"" + u.ID + "\" LogonName=\"" + u.LogonUserName + "\" SecProvider=\"" + u.LogonProvider + "\" SecProviderAlt=\"" + u.LogonProviderAlt + "\" />");

            sb.Append("</Root>");
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(sb.ToString());
            return xDoc;
        }



        public static XmlDocument GetGroupsList()
        {
            if (_URL == "")
                InitConfig();

            WebOrganization WS = new WebOrganization();
            WS.Url = _URL;
            if (_IntegratedSecurityMode)
            {
                WS.UseDefaultCredentials = true;
                WS.Credentials = System.Net.CredentialCache.DefaultCredentials;
            }
            else
            {
                WS.Credentials = new System.Net.NetworkCredential(_UserName, _Password);
            }

            StringBuilder sb = new StringBuilder();
            sb.Append("<Root Path=\"Qflow\">");

            Group[] groups = WS.GetGroups();
            foreach (Group g in groups)
            {
                sb.Append("<Node Name=\"" + g.Name  + "\" ID=\"" + g.ID + "\" ADPath=\"" + g.GroupADPath + "\" />");
            }

            sb.Append("</Root>");
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(sb.ToString());
            return xDoc;
        }

        public static XmlDocument GetGroup(int GroupID)
        {
            if (_URL == "")
                InitConfig();

            WebOrganization WS = new WebOrganization();
            WS.Url = _URL;
            if (_IntegratedSecurityMode)
            {
                WS.UseDefaultCredentials = true;
                WS.Credentials = System.Net.CredentialCache.DefaultCredentials;
            }
            else
            {
                WS.Credentials = new System.Net.NetworkCredential(_UserName, _Password);
            }

            StringBuilder sb = new StringBuilder();
            sb.Append("<Root Path=\"Qflow\">");

            Group g = WS.GetGroup(GroupID);
            sb.Append("<Node Name=\"" + g.Name + "\" ID=\"" + g.ID + "\" ADPath=\"" + g.GroupADPath + "\" />");
            sb.Append("</Root>");
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(sb.ToString());
            return xDoc;
        }
    }
}
